//
//  VC_ForgetPasswordCode.m
//  yjw-winner
//
//  Created by 隆霁 on 2018/9/20.
//  Copyright © 2018年 Shanghai decade financial information service co., LTD. All rights reserved.
//

#import "VC_ForgetPasswordCode.h"
#import "VC_ForgetPasswordSetNew.h"

@interface VC_ForgetPasswordCode ()
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UIButton *sendCodeButton;
@property (weak, nonatomic) IBOutlet UIButton *makesureButton;
@property (weak, nonatomic) IBOutlet UILabel *phoneNumberLabel;

@end

@implementation VC_ForgetPasswordCode
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSInteger index = [CountDownManager manager].currentCount;
    if (index == 60) {
        [self.sendCodeButton setTitle:@"发送验证码" forState:UIControlStateNormal];
    }else {
        [self countDown];
    }
}

- (void)countDown {
    __weak typeof(self) weakself = self;
    [[CountDownManager manager] startWithCountDownManagerBlock:^(NSInteger number) {
        if (number > 0 && number < 60) {
            [weakself.sendCodeButton setTitle:[NSString stringWithFormat:@"%ld", number] forState:UIControlStateNormal];
        }else {
            [weakself.sendCodeButton setTitle:@"发送验证码" forState:UIControlStateNormal];
        }
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.makesureButton.layer.masksToBounds = YES;
    self.makesureButton.layer.cornerRadius = 4.0f;
    @try {
        NSString *t = [NSString stringWithFormat:@"请使用安全手机%@获取短信验证码", [self dealwithPhoneNumber:_phoneNumber]];
        NSMutableAttributedString *mText = [[NSMutableAttributedString alloc] initWithString:t];
        [mText addAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} range:[t rangeOfString:[self dealwithPhoneNumber:_phoneNumber]]];
    }
    @catch (NSException *exception) {
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSString *)dealwithPhoneNumber:(NSString *)phoneNumber {
    return [phoneNumber stringByReplacingOccurrencesOfString:[phoneNumber substringWithRange:NSMakeRange(3, 6)] withString:@"******"];
}
#pragma mark --mark 验证验证码
- (IBAction)makesureCodeButtonClick:(UIButton *)sender {
    // 验证短信验证码
    // 跳转到下一个界面
    if (self.textField.text.length > 0) {
        VC_ForgetPasswordSetNew *vc = [[UIStoryboard storyboardWithName:@"LoginStoryBoard" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:NSStringFromClass([VC_ForgetPasswordSetNew class])];
        [self.navigationController pushViewController:vc animated:YES];
    }else {
        [MBProgressHUD showSuccess:@"请输入正确的验证码"];
    }
}
- (IBAction)sendSMSButtonClick:(UIButton *)sender {
    if ([CountDownManager manager].currentCount == 60) {
        // 获取短信
        // 倒计时
        [self countDown];
    }else {
        
    }
}

@end
